/*
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * openFuyao is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

package config

import (
	"testing"
)

func TestParsePathList(t *testing.T) {
	tests := []struct {
		name     string
		input    string
		expected []string
	}{
		{"single path", "/tmp", []string{"/tmp"}},
		{"multiple paths", "/tmp,/var,/opt", []string{"/tmp", "/var", "/opt"}},
		{"paths with spaces", " /tmp , /var , /opt ", []string{"/tmp", "/var", "/opt"}},
		{"empty string", "", []string{}},
		{"only commas", ",,,", []string{}},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			result := parsePathList(tt.input)
			assertEqual(t, len(result), len(tt.expected))
			for i := range result {
				assertEqual(t, result[i], tt.expected[i])
			}
		})
	}
}

func TestValidateConfig(t *testing.T) {
	tests := []struct {
		name    string
		config  AppConfig
		wantErr bool
	}{
		{
			name: "valid query config",
			config: AppConfig{
				Mode:         ModeQuery,
				Paths:        []string{"/tmp"},
				OutputFormat: "text",
			},
			wantErr: false,
		},
		{
			name: "invalid mode",
			config: AppConfig{
				Mode:  OperationMode("invalid"),
				Paths: []string{"/tmp"},
			},
			wantErr: true,
		},
		{
			name: "no paths for query mode",
			config: AppConfig{
				Mode:  ModeQuery,
				Paths: []string{},
			},
			wantErr: true,
		},
		{
			name: "no program for programCheck mode",
			config: AppConfig{
				Mode:        ModeProgramCheck,
				ProgramList: []string{},
			},
			wantErr: true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			err := validateConfig(&tt.config)
			if tt.wantErr {
				assertError(t, err)
			} else {
				assertNoError(t, err)
			}
		})
	}
}

func assertNoError(t *testing.T, err error) {
	t.Helper()
	if err != nil {
		t.Fatalf("Expected no error, got: %v", err)
	}
}

func assertError(t *testing.T, err error) {
	t.Helper()
	if err == nil {
		t.Fatal("Expected error, got nil")
	}
}

func assertEqual(t *testing.T, actual, expected interface{}) {
	t.Helper()
	if actual != expected {
		t.Fatalf("Expected %v, got %v", expected, actual)
	}
}
